<?php
/**
 * 手机端首页控制
 *
 *by wansyb QQ群：111731672
 *你正在使用的是由网店 运 维提供S2.0系统！保障你的网络安全！ 购买授权请前往shopnc
 */
defined('ByShopWWI') or exit('Access Invalid!');
class indexControl extends mobileHomeControl {
	public function __construct() {
		parent::__construct();
	}	/**
	 * 首页
	 */
	public function indexOp() {
		$model_mb_special = Model('mb_special');
		$data = $model_mb_special -> getMbSpecialIndex();
		$this -> _output_special($data, $_GET['type']);
	}	/**
	 * 专题
	 */
	public function specialOp() {
		$model_mb_special = Model('mb_special');
		$data = $model_mb_special -> getMbSpecialItemUsableListByID($_GET['special_id']);
		$datas['special_desc'] = $model_mb_special -> getMbSpecialdesc($_GET['special_id']);
		$this -> _output_special($data, $_GET['type'], $_GET['special_id'], $datas);
	}	/**
	 * 输出专题
	 */
	private function _output_special($data, $type = 'json', $special_id = 0, $datas = array()) {
		$model_special = Model('mb_special');
		if ($_GET['type'] == 'html') {
			$html_path = $model_special -> getMbSpecialHtmlPath($special_id);
			if (!is_file($html_path)) {
				ob_start();
				Tpl::output('list', $data);
				Tpl::showpage('mb_special');
				file_put_contents($html_path, ob_get_clean());
			}
			header('Location: ' . $model_special -> getMbSpecialHtmlUrl($special_id));
			die ;
		} else {
			$datas['list'] = $data;
			$datas['special_id'] = $special_id;
			$datas = $datas;
			output_data($datas);
		}
	}	/**
	 * 默认搜索词列表
	 */
	public function search_key_listOp() {
		//热门搜索
		$list = @explode(',', C('hot_search'));
		if (!$list || !is_array($list)) {
			$list = array();
		}
		//历史搜索
		if (cookie('his_sh') != '') {
			$his_search_list = explode('~', cookie('his_sh'));
		}
		$data['list'] = $list;
		$data['his_list'] = is_array($his_search_list) ? $his_search_list : array();
		output_data($data);
	}	/**
	 * 热门搜索列表
	 */
	public function search_hot_infoOp() {
		//热门搜索
		if (C('rec_search') != '') {
			$rec_search_list = @unserialize(C('rec_search'));
			$rec_value = array();
			foreach ($rec_search_list as $v) {
				$rec_value[] = $v['value'];
			}
		}
		output_data(array('hot_info' => $result ? $rec_value : array()));
	}	/**
	 * 高级搜索
	 */
	public function search_advOp() {
		$area_list = Model('area') -> getAreaList(array('area_deep' => 1), 'area_id,area_name');
		if (C('contract_allow') == 1) {
			$contract_list = Model('contract') -> getContractItemByCache();
			$_tmp = array();
			$i = 0;
			foreach ($contract_list as $k => $v) {
				$_tmp[$i]['id'] = $v['cti_id'];
				$_tmp[$i]['name'] = $v['cti_name'];
				$i++;
			}
		}
		output_data(array('area_list' => $area_list ? $area_list : array(), 'contract_list' => $_tmp));
	}	/*显示门店*/
	public function showmdOp() {
		$model_mb_user_token = Model('mb_user_token');
		$key = $_POST['key'];
		if (empty($key)) {
			$key = $_GET['key'];
		}
		$mb_user_token_info = $model_mb_user_token -> getMbUserTokenInfoByToken($key);
		$model_member = Model('member');
		$member_info = $model_member -> getMemberInfoByID($mb_user_token_info['member_id']);
		$model_store = Model('store');
		//exit(print($member_info['member_id']));
		$store_id = $model_store -> where(array('member_id' => $member_info['member_id'])) -> getField('store_id');
		$model_chain = Model('chain');
		$chain_list = $model_chain -> where(array('store_id' => $store_id[0])) -> Select();
		if (empty($chain_list)) {
			output_error('暂无门店');
		} else {
			output_data(array('chain_list' => $chain_list ? $chain_list : array()));
		}
	}	/**
	 * android客户端版本号
	 */
	public function shoukuanOp() {
	
		// echo $_POST['member_name'];
		//获取页面参数值
		//$member_id = $_POST['member_id'];//消费会员账号id
		
		$member_name = $_POST['member_name'];//消费会员账号
		$chain_id = $_POST['chain_id'];
		$chain_pwd = $_POST['pwd'];
		$member_amount = abs($_POST['member_amount']);//消费金额
		$pamount = $member_amount;
		$member_points = $member_amount * 100;//计算消费豆
		$pamount_sm = $_POST['member_shuming'];//消费备注说明
		$xf_date = strtotime(date("Y-m-d H:i:s", time()));
		$pl_addtime = strtotime(date("Y-m-d H:i:s"));
		
		//获取商家mb_user_token			
	    
		$key = $_POST['key'];
    	if (empty($key)) {
            $key = $_GET['key'];
	    }
        //通过商家门店id获取商家店铺名称信息
		
		$model_chain = Model('chain');
		$chain_list = $model_chain -> where(array('chain_id' => $chain_id)) -> Select();
		
		$store_id = $chain_list[0]['store_id'];
		$sjinfo = Model('store')->getStoreInfoByID($store_id);
		
		$sj_store_name=$sjinfo['member_name'];	
		//
		$model_mb_user_token = Model('mb_user_token');
		$mb_user_token_info = $model_mb_user_token -> getMbUserTokenInfoByToken($key);	
		//定义操作模型
		$model_member = Model('member');
        
		//根据mb_user_token获取商家账号信息
		
		$sj_member_info = $model_member -> getMemberInfoByID($mb_user_token_info['member_id']);
		
		$sj_member_name = $sj_member_info['member_name'];		//商家店铺会员账号
		
		$sj_member_id = $sj_member_info['member_id'];		//商家店铺会员id
		
		$sj_invite_one = $sj_member_info['invite_one'];
		$sj_invite_two = $sj_member_info['invite_two'];
		$sj_invite_three = $sj_member_info['invite_three'];
		$sj_kcpoints = $sj_member_info['kucun_points'];
		//商家会员账号推荐代理人id
		$agent_store_rec_id = $sj_member_info['agent_store_rec_id'];
		//获取消费会员信息		 
		// $member_info_xf = $model_member->getMemberInfo(array('member_id' => $member_id));
		$member_info_xf = $model_member->getMemberInfo(array('member_name' => $member_name));
		$member_id = $member_info_xf['member_id'];
		
		//$member_name = $member_info_xf['member_name'];
		$invite_one = $member_info_xf['invite_one'];
		$invite_two = $member_info_xf['invite_two'];
		$invite_three = $member_info_xf['invite_three'];
		//获取会员经验值
		$vip = $member_info_xf['member_exppoints'];
		//检测门店密码
		$where = array();
		$where['chain_id'] = $chain_id;
		$where['chain_pwd'] = md5($chain_pwd);
		$chain_info = Model('chain') -> getChainInfo($where);
		if (!empty($chain_info)) {
			/*                $_SESSION['chain_login']    = 1;
			 $_SESSION['chain_id']       = $chain_info['chain_id'];
			 $_SESSION['chain_store_id'] = $chain_info['store_id'];
			 $_SESSION['chain_user']     = $chain_info['chain_user'];
			 $_SESSION['chain_name']     = $chain_info['chain_name'];
			 $_SESSION['chain_img']      = getChainImage($chain_info['chain_img'], $chain_info['store_id']);
			 $_SESSION['chain_address']  = $chain_info['area_info'] . ' ' . $chain_info['chain_address'];
			 $_SESSION['chain_phone']    = $chain_info['chain_phone'];
			 */
			 //
			$chain_name = $chain_info['chain_name'];//门店名称
			$store_id = $chain_info['store_id'];//店铺id
			$chain_user = $chain_info['chain_user'];//店铺id
			
			//判断是否是自己的账号
			if($sj_member_id==$member_id){
				
				output_error("不能够买自己店铺的商品！");
				
				exit();
			}
			
			
			//检测库存积分是否充足
			if ($sj_kcpoints < $member_points) {
				output_error("您有" . $sj_kcpoints . "库存积分，不足以支付" . $member_points . "宝豆，请先购买库存积分。");
				exit();
			}
			
			if ($member_id) {
            
			// 添加门店消费记录
            $sql_xf = "insert into shopwwi_xiaofei (member_id,chain_id,store_id,member_name,pamount_num,pamount_sm,pamount_date) values({$member_id},{$chain_id},{$store_id},'{$member_name}',{$pamount},'{$pamount_sm}',{$pl_addtime})";
            //output_error("insert into shopwwi_xiaofei (member_id,chain_id,store_id,member_name,pamount_num,pamount_sm,pamount_date) values({$member_id},{$chain_id},{$store_id},'{$member_name}',{$pamount},'{$pamount_sm}',{$pl_addtime})");
            //output_error("中断提示");
			//exit();
			Model()->execute($sql_xf);
            // 增加会员消费豆
            Model()->execute('update shopwwi_member set member_points = member_points + ' . $member_points . ' where member_id=' . $member_id);
            //添加会员消费豆日志
			//pl_stage='consume'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$member_id},'{$member_name}',{$member_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，宝豆返还-宝豆增加','storepoints')";
            Model()->execute($sql_bc);
            //修改商家库存积分
            $m_points = "-" . $member_points;
            Model()->execute('update shopwwi_member set kucun_points = kucun_points - ' . $member_points . ' where member_id=' . $sj_member_id);
            //添加商家库存积分日志
			//pl_stage='kucunpointssell'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$sj_member_id},'{$sj_member_name}',{$m_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，商家" . $sj_member_name . "的库存积分使用-库存积分减少','kucunpoints')";
            Model()->execute($sql_bc);
            //商家的会员账号返16%
            $shiliu = $member_points * 16 / 100;
			
            // 增加商家16%的返还库存使用积分			
            Model()->execute('update shopwwi_member set member_points = member_points + ' . $shiliu . ' where member_id=' . $sj_member_id);
            
			// 增加商家16%的返还库存使用积分日志\
			//pl_stage='storeback'
            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$sj_member_id},'{$sj_member_name}',{$shiliu},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，商家" . $sj_member_name . "的库存积分使用-商家16%的宝豆返还-宝豆增加','kucunpoints')";
            Model()->execute($sql_bc);
			
              if ($invite_one > 0) {
               //获取消费会员一级推荐人信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $invite_one));
				
				$invite_member_name = $member_invite_info['member_name'];
                $invite_member_id = $member_invite_info['member_id'];
				
                //获取消费会员一级推荐人等级返现参数
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                
				//计算返佣分红豆pamount=会员消费金额
                $fy_huipoints = $pamount * $member_gradeinfo['nxmoney'];
				//消费会员一级推荐会员分红豆增加
                Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_huipoints . ' where member_id=' . $invite_member_id);
                //消费会员一级推荐会员分红豆日志
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$fy_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."' )";
				Model()->execute($sql_bc);
              }
              
			  if ($invite_two > 0) {
                //获取消费会员二级推荐人信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $invite_two));
				
				$invite_member_name = $member_invite_info['member_name'];
                $invite_member_id = $member_invite_info['member_id'];
				//获取消费会员一级推荐人等级返现参数
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
				//计算返佣分红豆pamount=会员消费金额
                $fy_huipoints = $pamount * $member_gradeinfo['nnxmoney'];
                //消费会员二级推荐会员分红豆增加
				 Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_huipoints . ' where member_id=' . $invite_member_id);
                //消费会员二级推荐会员分红豆日志增加
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$fy_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，上上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."' )";
				Model()->execute($sql_bc);
               }
                //以下是商家
			//一级推荐分红豆提成
                if ($sj_invite_one > 0) {
                    $member_invite_info = $model_member->getMemberInfo(array('member_id' => $sj_invite_one));
					
                    $invite_member_name = $member_invite_info['member_name'];
                    $invite_member_id = $member_invite_info['member_id'];
					
                    $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                    $sj_huipoints = $pamount * $member_gradeinfo['sale_tc'];
                   
				   //分红豆
				     Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $sj_huipoints . ' where member_id=' . $invite_member_id);
                    //分红豆日志
                //pl_stage='invitepoints'
                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$sj_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，" . $sj_member_name . "商家的上级推荐会佣金分提成-分红豆增加','redpoints','". $member_id ."')";
                    Model()->execute($sql_bc);
                }
                 //商家二级推荐分红豆提成
                if ($sj_invite_two > 0) {
                    $member_invite_info = $model_member->getMemberInfo(array('member_id' => $sj_invite_two));
                    $invite_member_name = $member_invite_info['member_name'];
                    $invite_member_id = $member_invite_info['member_id'];
					
					$member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                    //商家推荐会员提成（分红豆）
                    $sj_huipoints = $pamount * $member_gradeinfo['nsale_tc'];
                    //商家二级推荐分红豆提成
					Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $sj_huipoints . ' where member_id=' . $invite_member_id);
                    //商家二级推荐分红豆提成日志
                //pl_stage='invitepoints'
                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$invite_member_id},'{$invite_member_name}',{$sj_huipoints},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，" . $sj_member_name . "商家的上上级推荐会员佣金提成-分红豆增加','redpoints','". $member_id ."')";
                    Model()->execute($sql_bc);
                }
				 
				 //以下是代理商的提成
            if ($agent_store_rec_id > 0) {
				//获取代理商信息
                $member_invite_info = $model_member->getMemberInfo(array('member_id' => $agent_store_rec_id));
                $agent_member_name = $member_invite_info['member_name'];
                $agent_member_id = $member_invite_info['member_id'];
				$agent_member_exppoints = $member_invite_info['member_exppoints'];
				if ($agent_member_exppoints = 20999 ) {
			    //代理商提成参数
			    $agent_storetc=0.5;
				
				//$member_gradeinfo = $model_member->getOneMemberGrade(intval($member_invite_info['member_exppoints']));
                //计算代理商提成（分红豆）
			    //$fy_hui_points = $pamount * $member_gradeinfo['sale_tc'];
				$fy_hui_points = $pamount * $agent_storetc;
				//代理商分红豆增加
				Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_hui_points . ' where member_id=' . $agent_member_id);
                //代理商分红豆日志
                //pl_stage='invitepoints'
                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$agent_member_id},'{$agent_member_name}',{$fy_hui_points},{$pl_addtime},'[" . $member_name . "]在[" . $sj_store_name . "]的[" . $chain_name . "]门店WAP消费" . $pamount . "元，" . $sj_member_name . "的推荐代理商佣金提成-分红豆增加','redpoints','". $member_id ."')";
                Model()->execute($sql_bc);
				}
            }
			
				output_data("收银成功！");
				redirect('wap/tmpl/member/options.html');
				//showDialog('登录成功', 'wap/tmpl/member/options.html', 'succ');
				// ********************************************
			} else {
				output_error("会员账号不存在！");
			}
		} else {
			output_error('门店密码不正确！');
		}
	}	/**
	 * android客户端版本号
	 */
	public function apk_versionOp() {
		$version = C('mobile_apk_version');
		$url = C('mobile_apk');
		if (empty($version)) {
			$version = '';
		}
		if (empty($url)) {
			$url = '';
		}
		output_data(array('version' => $version, 'url' => $url));
	}}